/**
 * Created by user on 2015/10/23.
 */


angular.module('mis.pushManager.controllers', [])
    .controller('pushCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {

        $scope.showhNoticeCount = function (){

            if(!$scope.mode){alert('请选择推送信息方式!');return;};
            if(!$scope.type){alert('请选择推送用户类别!');return;};

            if($scope.message) {
                if ($scope.message.length > 22) {
                    alert('推送内容不超过22个字符');
                    return;
                };
            };
            //var parames = getQueryParames("http://127.0.0.1/e/action/ShowInfo.php?classid=9&id=2");
            //console.debug(parames)

            var opt = setPostData();
            if(opt.err){
                alert(opt.err);
                return;
            };

            console.debug(opt);
            if($scope.mode == 1 && $scope.type == 10){
                setNotice(opt.message, opt.uList.length, opt);
            }else if($scope.mode == 2 && $scope.type == 10){
                setNotice(opt.message, opt.uList.length, opt);
            }else if($scope.mode == 3 && $scope.type == 10){
                setNotice(opt.message, opt.uList.split(',').length, opt);
            } else {
                $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/push/pushNotice', opt, {
                    headers: {'Content-Type': 'application/json;charset=utf-8'}
                }).success(function (data) {
                    setNotice(opt.message, data.data[0].ucount, opt);
                }).error(function () {

                });
            };
        };

        var setPostData = function() {

            var opt = {
                mode : $scope.mode
                ,type : $scope.type
                ,openType : $scope.openType
            };

            switch($scope.mode) {
                case '1':   //系统通知

                    if ($scope.message) {
                        opt.message = $scope.message;
                    } else {
                        //alert('请填写推送内容!');
                        opt.err = '请填写推送内容!';
                        return opt;
                    };

                    var contentId;
                    //var openType = 3;

                    if ($scope.url) {
                        opt.url = $scope.url;

                        if($scope.openType ==3){
                            contentId = getQueryString($scope.url, 'activityId');
                        }else if($scope.openType ==4){
                            contentId = getQueryString($scope.url, 'topicId');
                        }

                        //if(opt.url){
                        //    contentId = getQueryString(opt.url,'activityId');
                        //    if(!contentId){
                        //        contentId = getQueryString(opt.url,'topicId');
                        //    }
                        //};

                        if(contentId)opt.contentId =  contentId;

                        ////获取url第一个参数
                        //var parames = getQueryParames($scope.url);
                        //if (!$.isEmptyObject(parames)) {
                        //    var index = 0;
                        //    for (key in parames) {
                        //        if (index == 0)contentId = parames[key];
                        //        index++;
                        //    };
                        //}
                    } else {
                        //alert('请填写跳转页面url!');
                        if($scope.openType ==3 || $scope.openType==4) {
                            opt.err = '请填写跳转页面url!';
                            return opt;
                        };

                        if($scope.openType ==1){
                            opt.contentId = $scope.question;
                        };
                    };

                    switch($scope.type)
                    {
                        case '1'://雷锋团成员
                            opt.userRole  =3;
                            break;
                        case '2'://小区版主
                            opt.userRole  =1;
                            break;
                        case '3'://7天内新注册用户
                            break;
                        case '4'://3天未上线用户
                            break;
                        case '5'://7天未上线用户
                            break;
                        case '6'://2周未上线用户

                            break;
                        case '7'://导入用户

                            break;
                        case '8'://选择小区
                            break;
                        case '9'://省市用户
                            break;
                        case '10'://特定用户
                            if($scope.sendNumber) {
                                var sendNumber = $scope.sendNumber.replace(/\ +/g, "").replace(/[ ]/g, "").replace(/[\r\n]/g, "");
                                opt.uList = sendNumber.split(',');
                            }else{
                                opt.err = '请填写推送用户!';
                                return opt;
                            };

                            break;
                        case '13'://注册用户
                            opt.beginDate = $scope.beginDate;
                            opt.endDate = $scope.endDate;

                            if (opt.beginDate && opt.endDate && opt.beginDate > opt.endDate) {
                                opt.err = '开始时间不能大于结束时间';
                                return opt;
                            };
                            if(!opt.beginDate && !opt.endDate){
                                opt.err = '请选择注册日期';
                                return opt;
                            }

                            break;
                        default:

                    };

                    break;
                case '2':   //私信
                    if($scope.letter){
                        opt.message = $scope.letter;
                    }else{
                        //alert('请填写私信内容!');
                        opt.err = '请填写私信内容!';
                        return opt;
                    };

                    switch ($scope.type){

                        case '10'://特定用户
                            if($scope.sendNumber) {
                                var sendNumber = $scope.sendNumber.replace(/\ +/g, "").replace(/[ ]/g, "").replace(/[\r\n]/g, "");
                                opt.uList = sendNumber.split(',');
                            }else{
                                opt.err = '请填写推送用户!';
                                return opt;
                            };

                            break;

                        case '13'://注册用户
                            opt.beginDate = $scope.beginDate;
                            opt.endDate = $scope.endDate;

                            if (opt.beginDate && opt.endDate && opt.beginDate > opt.endDate) {
                                opt.err = '开始时间不能大于结束时间';
                                return opt;
                            };
                            if(!opt.beginDate && !opt.endDate){
                                opt.err = '请选择注册日期';
                                return opt;
                            }

                            break;
                        default :

                    }

                    break;
                case '3':   //短信
                    if($scope.sms){
                        opt.message = $scope.sms;
                    }else{
                        //alert('请填写短信内容!');
                        opt.err = '请填写短信内容!';
                        return opt;
                    };

                    switch($scope.type)
                    {
                        case '1'://雷锋团成员
                            opt.userRole  =3;
                            break;
                        case '2'://小区版主
                            opt.userRole  =1;
                            break;
                        case '3'://7天内新注册用户
                            break;
                        case '4'://3天未上线用户
                            break;
                        case '5'://7天未上线用户
                            break;
                        case '6'://2周未上线用户

                            break;
                        case '7'://导入用户

                            break;
                        case '8'://选择小区
                            break;
                        case '9'://省市用户
                            break;
                        case '10'://特定用户
                            if($scope.sendNumber) {
                                var sendNumber = $scope.sendNumber.replace(/\ +/g, "").replace(/[ ]/g, "").replace(/[\r\n]/g, "");
                                opt.uList = sendNumber;
                            }else{
                                opt.err = '请填写推送用户!';
                                return opt;
                            };

                            break;
                        case '13'://注册用户
                            opt.beginDate = $scope.beginDate;
                            opt.endDate = $scope.endDate;

                            if (opt.beginDate && opt.endDate && opt.beginDate > opt.endDate) {
                                opt.err = '开始时间不能大于结束时间';
                                return opt;
                            };
                            if(!opt.beginDate && !opt.endDate){
                                opt.err = '请选择注册日期';
                                return opt;
                            };

                            break;
                        default:
                            opt.err = '操作无效，请联系管理员！';
                            return opt;
                    };

                    break;
                default:


                    return opt;
            };

            return opt;
        };

        //设置推送提醒
        var setNotice = function (msg, count,option){
            $scope.messsage = {
                sendMsg :msg,
                count: count,
                option : option
            };

            $('#showPushNotice').modal({target: '#showPushNotice', width: 800, height: 360 });
        };


        $scope.pushMsg = function(){

            $('#showPushNotice').modal("close");
            $('#loading').modal("close");

            console.debug($scope.messsage.option);

            var opt = $scope.messsage.option;

            if(opt.url) {
                var parm = getQueryString(opt.url, 'topicId');
                if (parm) {
                    //$http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/Systems/shortUrl', {url: opt.url}, {
                    //    headers: {'Content-Type': 'application/json;charset=utf-8'}
                    //}).success(function (data) {
                    //opt.url = data.data;
                    opt.url = opt.url.substr(0, opt.url.indexOf('?')) //+ '?source=notify';
                };

            };


            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/push/pushMessage',opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if (data.result.status == 1) {
                    $('#loading').modal("close");
                    alert("消息推送成功！");
                } else {
                    $('#loading').modal("close");
                    alert("消息推送失败！");
                    //$scope.pagination.selectPage($scope.currentPage);
                }
                ;
            }).error(function () {
                $('#loading').modal("close");
                alert("消息推送失败！");
            });

                //}).error(function (err) {
                //    $('#loading').modal("close");
                //    alert("获取短连接失败！");
                //    //$scope.pagination.selectPage($scope.currentPage);
                //});;

            //
            //}else {
            //
            //    $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/push/pushMessage',opt, {
            //        headers: {'Content-Type': 'application/json;charset=utf-8'}
            //    }).success(function (data) {
            //        if (data.result.status == 1) {
            //            $('#loading').modal("close");
            //            alert("消息推送成功！");
            //        } else {
            //            $('#loading').modal("close");
            //            alert("消息推送失败！");
            //            //$scope.pagination.selectPage($scope.currentPage);
            //        }
            //        ;
            //    }).error(function () {
            //        $('#loading').modal("close");
            //        alert("消息推送失败！");
            //    });
            //}
        }


    }]) .controller('pushAnalysis', ['$scope','$http', '$location', function ($scope, $http, $location) {



        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/push/pushMessageList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.resultList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                console.debug($scope.pushList);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $scope.pushList = null;
            },
            params: {}
        });


        $scope.search = function(temp) {
            var current = temp.target;

            if (current) {
                $scope.currentPage = $scope.pagination.currentPage;
            }else {
                $scope.pagination.currentPage = 1;
                $scope.currentPage = 1;
            }

            var body = {};
            if ($scope.pushTitle) body.title = $scope.pushTitle;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/push/pushMessageList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                $scope.resultList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $('#loading').modal("close");
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;

            }).error(function (err) {
                $('#loading').modal("close");
                $scope.resultList = null;
            });
        };

    }]);